<%--
  Created by IntelliJ IDEA.
  User: 邱嘉丽
  Date: 2024/3/24
  Time: 20:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="entity.PersonBean" %>
<%@ page import="java.util.List" %>
<%@ page import="entity.Page" %>
<%
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=UTF-8");
%>
<html>
<head>
    <title>Title</title>
    <style>
        @import url("../css/person_listStyle.css");
    </style>
    <script>
        function resetForm() {
            document.getElementsByName("person_number")[0].value = "";
            document.getElementsByName("name")[0].value = "";
            document.getElementsByName("degree")[0].value = "";
            document.getElementsByName("person_position")[0].value = "";
            document.getElementsByName("tel")[0].value = "";
        }
        function deleteData(id) {
            if (confirm("确定删除吗？")) {
                // 发送删除请求到服务器
                window.location.href = "<%=request.getContextPath()%>/group/people/person_delete?id=" + id;
            }
        }
    </script>
</head>
<body>
<div>
    <div id="headDiv">
        <div id="logo">
            <img src="../img/logo_search.png" width="30" height="30" alt=""/>
        </div>
        <div id="title">人员查询</div>
    </div>
    <div>
        <form action="personList">
            <%
                String person_number = (String) request.getAttribute("person_number");
                String person_name = (String) request.getAttribute("person_name");
                String degree = (String) request.getAttribute("degree");
                String person_position = (String) request.getAttribute("person_position");
                String tel = (String) request.getAttribute("tel");
                if (person_number == null)
                    person_number = "";
                if (person_name == null)
                    person_name = "";
                if (degree == null)
                    degree = "";
                if (person_position == null)
                    person_position = "";
                if (tel == null)
                    tel = "";
            %>
            <div id="searchDiv">
                工号<input class="searchBorder" type="text" id="person_number" name="person_number" placeholder="请输入工号" value="<%=person_number%>">
                姓名<input class="searchBorder" type="text" id="name" name="name" placeholder="请输入姓名"
                           value="<%=person_name%>">
                最高学历<input class="searchBorder" type="text" id="degree" name="degree" placeholder="请输入最高学历"
                               value="<%=degree%>">
                职称<input class="searchBorder" type="text" id="person_position" name="person_position" placeholder="请输入职称"
                           value="<%=person_position%>">
                手机号<input class="searchBorder" type="text" id="tel" name="tel" placeholder="请输入手机号" value="<%=tel%>">
                <input type="submit" value="查找" class="bkg1">
                <button class="bkg2" onclick="resetForm()">重置</button>
                <button class="bkg3"><a href="personEdit">添加</a></button>
            </div>
        </form>
    </div>

    <div>
            <table border="1" cellpadding="3" cellspacing="0">
                <tr>
                    <th>工号</th>
                    <th>姓名</th>
                    <th>部门</th>
                    <th>入职时间</th>
                    <th>最高学历</th>
                    <th>专业</th>
                    <th>职称</th>
                    <th>手机号</th>
                    <th>家庭住址</th>
                    <th>个人简介</th>
                    <th>操作</th>
                </tr>
                <%
                    List<PersonBean> personList = (List<PersonBean>)request.getAttribute("personList");
                    if (personList != null){
                        for (PersonBean obj:personList){
                %>

                <tr>
                    <td><%=obj.getPerson_number()%></td>
                    <td><%=obj.getPerson_name()%></td>
                    <td><%=obj.getDepartment()%></td>
                    <td><%=obj.getDate()%></td>
                    <td><%=obj.getDegree()%></td>
                    <td><%=obj.getMajor()%></td>
                    <td><%=obj.getPerson_position()%></td>
                    <td><%=obj.getTelephone()%></td>
                    <td><%
                        if (obj.getAddress() == null)
                            out.println("");
                        else
                            out.println(obj.getAddress());
                    %></td>
                    <td><%
                        if (obj.getIntroduce() == null)
                            out.println("");
                        else
                            out.println(obj.getIntroduce());
                    %></td>
                    <td>
                        <button class="button1"><a href="personEdit?id=<%=obj.getId()%>">编辑</a></button>
                        <button class="button2" onclick="deleteData(<%=obj.getId()%>)">删除</button>
                    </td>
                </tr>
                <%
                        }
                    }
                %>
            </table>
    </div>
    <div class="pagination">
        <%Page person_Page=(Page)request.getAttribute("personPage");
            if(person_Page!=null){
        %>
        <% if(person_Page.getCurrentPage() > 1) { %>
        <button class="prev" onclick="goToPrevPage()">&laquo; 上一页</button>
        <% } %>

        <% for(int i = Math.max(1, person_Page.getCurrentPage() - 4); i <= Math.min(person_Page.getCurrentPage() + 4, person_Page.getTotalPage()); i++) { %>
        <button onclick="goToNum(<%= i %>)" <% if(i == person_Page.getCurrentPage()) { %>class="active"<% } %>><%= i %></button>
        <% } %>


        <% if(person_Page.getCurrentPage() < person_Page.getTotalPage()) { %>
        <button class="next" onclick="goToNextPage()">下一页 &raquo;</button>
        <% } %>

        <button onclick="goToFirst()">首页</button>
        <button onclick="goToLast()">尾页</button>
        <%}%>
    </div>
</div>
</body>
</html>

<script>
    function goToNum(i){

        <%if(person_Page!=null){%>
        // 这里可以实现跳转到下一页的逻辑，例如递增页数
        // 构建新的URL并重定向
        window.location.href = '?currentPage=' + i+"&person_number="+document.getElementById("person_number").value+"&name="+document.getElementById("name").value+"&degree="+document.getElementById("degree").value+"&person_position="+document.getElementById("person_position").value+"&tel="+document.getElementById("tel").value;

        <%}%>

    }
    function goToFirst(){
        <%if(person_Page!=null){%>
        // 这里可以实现跳转到下一页的逻辑，例如递增页数
        var nextPage = <%=1  %>;

        // 构建新的URL并重定向
        window.location.href = '?currentPage=' + nextPage +"&person_number="+document.getElementById("person_number").value+"&name="+document.getElementById("name").value+"&degree="+document.getElementById("degree").value+"&person_position="+document.getElementById("person_position").value+"&tel="+document.getElementById("tel").value;

        <%}%>

    }

    function goToLast(){
        <%if(person_Page!=null){%>
        // 这里可以实现跳转到下一页的逻辑，例如递增页数
        var nextPage = <%=person_Page.getTotalPage()  %>;

        // 构建新的URL并重定向
        window.location.href = '?currentPage=' + nextPage+"&person_number="+document.getElementById("person_number").value+"&name="+document.getElementById("name").value+"&degree="+document.getElementById("degree").value+"&person_position="+document.getElementById("person_position").value+"&tel="+document.getElementById("tel").value;

        <%}%>

    }
    function goToNextPage() {
        <%if(person_Page!=null){%>
        // 这里可以实现跳转到下一页的逻辑，例如递增页数
        var nextPage = <%= person_Page.getCurrentPage()+1  %>;


        // 构建新的URL并重定向
        window.location.href = '?currentPage=' + nextPage+"&person_number="+document.getElementById("person_number").value+"&name="+document.getElementById("name").value+"&degree="+document.getElementById("degree").value+"&person_position="+document.getElementById("person_position").value+"&tel="+document.getElementById("tel").value;

        <%}%>
    }

    function goToPrevPage(){
        <%if(person_Page!=null){%>
        // 这里可以实现跳转到下一页的逻辑，例如递增页数
        var nextPage = <%= person_Page.getCurrentPage()-1  %>;

        // 构建新的URL并重定向
        window.location.href = '?currentPage=' + nextPage+"&person_number="+document.getElementById("person_number").value+"&name="+document.getElementById("name").value+"&degree="+document.getElementById("degree").value+"&person_position="+document.getElementById("person_position").value+"&tel="+document.getElementById("tel").value;

        <%}%>
    }


</script>
